package com.example.controller.event;

import com.example.pojo.entity.event.Message;
import com.example.event.event.CustomEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/9/20 22:25
 */
@RestController
@RequestMapping(value = "push")
@Slf4j
public class DemoController {
    @Autowired
    ApplicationEventPublisher applicationEventPublisher;

    //http://localhost:8082/push?code=ok&message=美女爱我
    //http://localhost:8082/push?code=xx&message=我爱美女

    @GetMapping
    public String push(String code,String message){
        log.info("事件发布：{},{}",code,message);
        applicationEventPublisher.publishEvent(new CustomEvent(this,Message.builder().code(code).message(message).build()));
        log.info("发布成功：截止时间：{}",stampToDate(System.currentTimeMillis()));
        return "事件发布成功";
    }
    @GetMapping(value = "obj")
    public String pushObj(String code,String message){
        log.info("事件发布：{},{}",code,message);
        applicationEventPublisher.publishEvent(Message.builder().code(code).message(message).build());
        return "对象发布成功";
    }

    public static String stampToDate(long  s){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Timestamp t = new Timestamp(s);
        Date date = new Date(t.getTime());
        return simpleDateFormat.format(date);
    }
}
